package com.bytz.modules.cms.reseller.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.modules.cms.reseller.constant.ResellerConstant;
import com.bytz.modules.cms.reseller.entity.CouponReseller;
import com.bytz.modules.cms.reseller.mapper.CouponResellerMapper;
import com.bytz.modules.cms.reseller.service.ICouponResellerService;
import com.bytz.modules.cms.reseller.util.ResellerSecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 莱宝CMS—经销商管理—优惠券表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-11
 */
@Service
public class CouponResellerServiceImpl extends ServiceImpl<CouponResellerMapper, CouponReseller> implements ICouponResellerService {

    @Override
    public CouponReseller getValidCouponById(String id, String resellerId) {

        if (StringUtils.isBlank(id) || StringUtils.isBlank(resellerId)) {
            return null;
        }

        return lambdaQuery()
                .eq(CouponReseller::getId, id)
                .eq(CouponReseller::getResellerId, resellerId)
                .eq(CouponReseller::getStatus, ResellerConstant.CouponStatus.ENABLE)
                .lt(CouponReseller::getEffectStartTime, LocalDateTime.now())
                .gt(CouponReseller::getEffectEndTime, LocalDateTime.now())
                .one();
    }

    @Override
    public List<CouponReseller> getValidCouponByReseller(String resellerId) {

        return lambdaQuery()
                .eq(CouponReseller::getResellerId, resellerId)
                .eq(CouponReseller::getStatus, ResellerConstant.CouponStatus.ENABLE)
                .lt(CouponReseller::getEffectStartTime, LocalDateTime.now())
                .gt(CouponReseller::getEffectEndTime, LocalDateTime.now())
                .list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean markAsUsedById(String id) {

        return lambdaUpdate()
                .eq(CouponReseller::getId, id)
                .set(CouponReseller::getStatus, ResellerConstant.CouponStatus.USED)
                .update();
    }

    @Override
    public List<CouponReseller> getByLoginReseller() {
        return getValidCouponByReseller(ResellerSecurityUtils.getUserId());
    }

    @Override
    public List<CouponReseller> searchByCode(String code) {

        String resellerId = ResellerSecurityUtils.getUserId();

        return lambdaQuery()
                .eq(CouponReseller::getResellerId, resellerId)
                .like(CouponReseller::getCouponName, code)
                .eq(CouponReseller::getStatus, ResellerConstant.CouponStatus.ENABLE)
                .lt(CouponReseller::getEffectStartTime, LocalDateTime.now())
                .gt(CouponReseller::getEffectEndTime, LocalDateTime.now())
                .list();
    }

    @Override
    public CouponReseller getByIdAndLoginReseller(String id) {

        String resellerId = ResellerSecurityUtils.getUserId();

        return lambdaQuery()
                .eq(CouponReseller::getResellerId, resellerId)
                .like(CouponReseller::getId, id)
                .eq(CouponReseller::getStatus, ResellerConstant.CouponStatus.ENABLE)
                .lt(CouponReseller::getEffectStartTime, LocalDateTime.now())
                .gt(CouponReseller::getEffectEndTime, LocalDateTime.now())
                .one();
    }
}
